<%@page import="cn.ticket.util.StringUtil"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="cn.ticket.db.DBConnect"%>
<%@page import="java.sql.Connection"%>
<%@page import="cn.ticket.Constant"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
	request.setCharacterEncoding("utf8") ;
	String userId= (String)session.getAttribute("login_user_id") ;
	int counts = Integer.parseInt(request.getParameter("counts")) ;
	if(null==userId ||"".equals(userId) ){
		session.setAttribute("errorinfo", Constant.ERRORINFO_NETERROR) ;
		%>
		<jsp:forward page="/front/result.jsp"></jsp:forward>
		<%
	}else{
		try{
		String busId = request.getParameter("id") ;
		Connection con = new  DBConnect().getConnection() ;
		String sql="select * from bus where id=?" ;
		PreparedStatement pst = con.prepareStatement(sql) ;	
		pst.setString(1,busId) ;
		ResultSet rs = pst.executeQuery() ;
		if(rs.next()){
			sql = "insert into ticket(id,user_id,oriStation,desStation,time,creTime,status,num,price,counts) values(?,?,?,?,?,now(),?,?,?,?)" ;
			PreparedStatement pstmt = con.prepareStatement(sql) ;
			String id = StringUtil.createUUID() ;
			pstmt.setString(1, id) ;
			pstmt.setString(2, userId ); 
			pstmt.setString(3,rs.getString(3)) ;
			pstmt.setString(4, rs.getString(4)) ;
			pstmt.setString(5,rs.getString(6) ) ;
// 			pstmt.setDate(6, "now()") ;
			pstmt.setInt(6, 0) ;
			pstmt.setString(7, rs.getString(2)) ;
			pstmt.setInt(8, rs.getInt(5)) ;
			pstmt.setInt(9, counts) ;
			pstmt.executeUpdate() ;
			
			sql="update bus set remain=remain-"+counts+" where id='"+id+"'" ;
			PreparedStatement psd = con.prepareStatement(sql) ;
			psd.executeUpdate();
			%>
			<<jsp:forward page="orderList.jsp"></jsp:forward>
		<%
		}
	}catch(Exception e){
			e.printStackTrace() ;
			session.setAttribute("errorinfo", Constant.ERRORINFO_NETERROR)  ;
			%>
			 	<jsp:forward page="/front/result.jsp"></jsp:forward>
			<%
		}
	}
%>
